<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Service;

use App\Constants\ErrorCode;
use App\Dao\SendCodeLogDao;
use App\Dao\UserDao;
use App\Dao\UserRelationDao;
use App\Event\UserRegister;
use App\Utils\Jwt;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Utils\Parallel;

class UserService
{
    /**
     * @Inject
     * @var PhoneService
     */
    protected $phoneService;

    /**
     * @Inject
     * @var UserDao
     */
    protected $userDao;

    /**
     * @Inject
     * @var SendCodeLogDao
     */
    protected $sendCodeLogDao;

    /**
     * @Inject
     * @var UserRelationDao
     */
    protected $userRelationDao;

    /**
     * @Inject
     * @var Jwt
     */
    protected $jwt;

    /**
     * 用户登录.
     * @param $account
     * @param $code
     */
    public function userLogin($account, $code): array
    {
        $isOk = $this->phoneService->checkCode($account, $code);
        if (! $isOk) {
            throw new \Exception('', ErrorCode::CODE_ERROE);
        }
        $this->sendCodeLogDao->invalidCode($account, $code);

        $userInfo = $this->userDao->getUserByAccount($account);
        if (empty($userInfo)) {
            $userId = $this->userDao->addUser($account);
            $userInfo = [
                'user_id' => $userId,
            ];
            //event_dispatcher()->dispatch(new UserRegister($userInfo));
        }

        $payload = [
            'user_id' => $userInfo['user_id'],
        ];
        $token = $this->jwt->encode($payload);
        $this->userDao->addLoginLog($userInfo['user_id']);
        return [
            'token' => $token,
        ];
    }

    /**
     * 用户页面获取用户信息.
     * @param $userId
     * @return mixed
     */
    public function getUserInfo($userId): array
    {
        $parallel = new Parallel();
        $parallel->add(function () use ($userId) {
            return $this->userDao->getUserInfo($userId);
        }, 'user_info');
        $parallel->add(function () use ($userId) {
            return $this->userRelationDao->getFollowedNumber($userId);
        }, 'followed_number');
        $parallel->add(function () use ($userId) {
            return $this->userRelationDao->getFollowNumber($userId);
        }, 'follow_number');
        $ret = $parallel->wait();
        $userInfo = $ret['user_info'];
        $userInfo['followed_number'] = $ret['followed_number'];
        $userInfo['follow_number'] = $ret['follow_number'];
        return $userInfo;
    }

    /**
     * 获取关注列表.
     * @param $userId
     * @param mixed $start
     * @param mixed $limit
     * @param mixed $page
     */
    public function getFollowList($userId, $start, $limit): array
    {
        $list = $this->userRelationDao->getFollowList($userId, $start, $limit);
        if (! empty($list)) {
            $arr = [];
            foreach ($list as $value) {
                $value['is_followed'] = $this->userRelationDao->checkIsFollowMe($value['user_id'], $userId);
                $value['is_followed'] = strval(intval($value['is_followed']));
                $arr[] = [
                    'user_id' => $value['user_id'],
                    'name' => $value['user_name']['nickname'],
                    'is_followed' => $value['is_followed'],
                ];
            }
            $list = $arr;
        }
        return $list;
    }

    /**
     * 获取粉丝列表.
     * @param $userId
     * @param mixed $start
     * @param mixed $limit
     * @param mixed $page
     */
    public function getFollowedList($userId, $start, $limit): array
    {
        $list = $this->userRelationDao->getFollowedList($userId, $start, $limit);
        if (! empty($list)) {
            $arr = [];
            foreach ($list as $value) {
                $value['is_followed'] = $this->userRelationDao->checkIsFollowMe($userId, $value['user_id']);
                $value['is_followed'] = strval(intval($value['is_followed']));
                $arr[] = [
                    'user_id' => $value['user_id'],
                    'name' => $value['user_name']['nickname'],
                    'is_followed' => $value['is_followed'],
                ];
            }
            $list = $arr;
        }
        return $list;
    }
}
